package pl.vanta.utils.gui.tables.renderers;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

public class DateTimeRenderer extends DefaultTableCellRenderer
{
	private static final long	serialVersionUID	= 1;

	private DateFormat			dateFormat;

	public DateTimeRenderer(String pattern)
	{
		this(new SimpleDateFormat(pattern));
		
		this.dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
	}

	public DateTimeRenderer(DateFormat dateFormat)
	{
		this.dateFormat = dateFormat;
	}

	@Override
	public int getHorizontalAlignment()
	{
		return SwingConstants.RIGHT;
	}

	@Override
	protected void setValue(Object value)
	{
		String newValue = "";

		if (value instanceof Date)
		{
			newValue = this.dateFormat.format((Date) value);
		}
		else if (value instanceof Integer)
		{
			newValue = this.dateFormat.format(new Date((Integer) value));
		}
		else if (value instanceof Long)
		{
			newValue = this.dateFormat.format(new Date((Long) value));
		}

		super.setValue(newValue);
	}
}